package site.jlopen.controller.api;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson2.JSON;

import site.jlopen.assets.spring.ComponentsInit;
import site.jlopen.components.entity.GrapGroup;
import site.jlopen.components.entity.GraphNodeParam;
import site.jlopen.components.entity.GraphNodeParamCopy;
import site.jlopen.components.utils.GrapDataUtil;
import site.jlopen.entity.BaseResponse;
import site.jlopen.secret.model.graph.SecretGrapGroup;
import site.jlopen.secret.model.graph.SecretGraphNodeParam;
import site.jlopen.service.intf.project.GraphService;

@RestController
@RequestMapping(value="/api/grap")
public class ApiGrapController {
	private final Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	private ComponentsInit componentsInit;
	@Autowired
    private GraphService graphService;
	
	@PostMapping(value = "/groups" )
	public BaseResponse<List<SecretGrapGroup>> groups() {
		logger.info("组件接口调用");
		//List<GrapGroup> list = GrapDataUtil.getGrapGroup();
		List<GrapGroup> list =  graphService.getGrapGroup();
		List<SecretGrapGroup> res = new ArrayList<SecretGrapGroup>();
		if(null != list && list.size() > 0) {
			String json = JSON.toJSONString(list);
			res = JSON.parseArray(json, SecretGrapGroup.class);
		}
		return new BaseResponse<List<SecretGrapGroup>>().success("成功").setData(res);
	}
	
	@PostMapping(value = "/nodeAttr" )
	public BaseResponse<SecretGraphNodeParam> nodeAttr(String nodeCode) {
		logger.info("组件接口调用");
		GraphNodeParam defParam = componentsInit.getNode(nodeCode);
		SecretGraphNodeParam res = null;
		if(null != defParam) {
			GraphNodeParam param = new GraphNodeParamCopy().init(defParam);
			String json = JSON.toJSONString(param);
			res = JSON.parseObject(json, SecretGraphNodeParam.class);
		}
		return new BaseResponse<SecretGraphNodeParam>().success("成功").setData(res);
	}
}